package com.sgxy.wmservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sgxy.servicebase.exceptionhandler.ERPException;
import com.sgxy.wmservice.entity.WmInputOrder;
import com.sgxy.wmservice.entity.WmOutputOrder;
import com.sgxy.wmservice.entity.WmStoreGoods;
import com.sgxy.wmservice.entity.vo.OutputOrderVo;
import com.sgxy.wmservice.mapper.WmOutputOrderMapper;
import com.sgxy.wmservice.service.WmOutputOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgxy.wmservice.service.WmStoreGoodsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-29
 */
@Service
public class WmOutputOrderServiceImpl extends ServiceImpl<WmOutputOrderMapper, WmOutputOrder> implements WmOutputOrderService {

    @Autowired
    private WmStoreGoodsService wmStoreGoodsService;

    @Override
    public IPage<OutputOrderVo> selectPageVoList(IPage<OutputOrderVo> page, WmOutputOrder wmOutputOrder) {
        return baseMapper.selectPageVoList(page,wmOutputOrder);
    }

    @Override
    public OutputOrderVo selectOneById(String id) {
        OutputOrderVo outputOrderVo = baseMapper.selectOneById(id);

        return outputOrderVo;
    }

    @Override
    @Transactional//开启事务
    public void addOutputOrder(WmOutputOrder wmOutputOrder) {
        WmStoreGoods storeGoods;
        QueryWrapper<WmStoreGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id",wmOutputOrder.getStoreId());
        wrapper.eq("goods_id",wmOutputOrder.getGoodsId());
        storeGoods = wmStoreGoodsService.getOne(wrapper);
        System.out.println("---------------------------------"+storeGoods);
        if(storeGoods == null){
            storeGoods = new WmStoreGoods();
        }
        System.out.println("-+++++++++++++++++++++++++++++++++"+storeGoods);
        //判断出库数量是否大于库存
        if(storeGoods.getStockNum()<wmOutputOrder.getGoodsNum()){
            throw new ERPException(20001,"库存不足");
        }
        storeGoods.setStockNum(storeGoods.getStockNum()-wmOutputOrder.getGoodsNum());

        baseMapper.insert(wmOutputOrder);
        wmStoreGoodsService.updateStockNum(storeGoods);

    }
    @Override
    public void updateOutputOrder(WmOutputOrder wmOutputOrder) {
        WmStoreGoods storeGoods;
        QueryWrapper<WmStoreGoods> wrapper = new QueryWrapper<>();
        WmOutputOrder beforeWmOutputOrder = baseMapper.selectById(wmOutputOrder.getId());
        wrapper.eq("store_id",wmOutputOrder.getStoreId());
        wrapper.eq("goods_id",wmOutputOrder.getGoodsId());
        storeGoods = wmStoreGoodsService.getOne(wrapper);
        storeGoods.setStockNum(storeGoods.getStockNum()+beforeWmOutputOrder.getGoodsNum()-wmOutputOrder.getGoodsNum());
        wmStoreGoodsService.saveOrUpdate(storeGoods);
        baseMapper.updateById(wmOutputOrder);
    }

    @Override
    public void deleteById(String id) {
        WmStoreGoods storeGoods;
        QueryWrapper<WmStoreGoods> wrapper = new QueryWrapper<>();
        WmOutputOrder wmOutputOrder = baseMapper.selectById(id);
        wrapper.eq("store_id",wmOutputOrder.getStoreId());
        wrapper.eq("goods_id",wmOutputOrder.getGoodsId());
        storeGoods = wmStoreGoodsService.getOne(wrapper);
        storeGoods.setStockNum(storeGoods.getStockNum()-wmOutputOrder.getGoodsNum());
        wmStoreGoodsService.saveOrUpdate(storeGoods);
        baseMapper.deleteById(id);

    }
}
